﻿package org.lq.shopping.dao.impl;



import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.MenuDao;
import org.lq.shopping.entity.Menu;
import org.lq.shopping.util.DruidUtil;

/**
 *  菜单类  接口实现类
 * @author dj
 *
 */
public class MenuDaoImpl implements MenuDao {
	private QueryRunner  qr=new QueryRunner(DruidUtil.getDataSource());
	private Logger log=Logger.getLogger(MenuDaoImpl.class);
	/**
	 * 添加菜单
	 */
	@Override
	public int add(Menu t) {
		log.info("添加菜单");
		String  sql="insert into SHOP_MENU"
				+ "(MENU_SEQ.nextval,menu_name,menu_describe,"
				+ "menu_address,menu_parentid,menu_type) "
				+ "values( ? , ? , ? , ? , ? , ? )";
		int count=0;
		try {
			count=qr.execute(sql, t.getMenu_name(),
				 t.getMenu_describe(),	t.getMenu_address(),
				 t.getMenu_parentid(),t.getMenu_type());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
		log.error(e.toString());
		}
	
		log.debug("传入参数["+t.getMenu_name()+","+
				 t.getMenu_describe()+","+	t.getMenu_address()+","+
				 t.getMenu_parentid()+","+t.getMenu_type()+"]");	
		return count;
	}
/**
 * 删除菜单
 */
	@Override
	public int delete(int id) {
		log.info("删除菜单");
		String sql="delete  from SHOP_MENU where menu_id= ?";
		int count=0;
		try {
			count=qr.execute(sql, id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("传入参数["+id+"]");
		return count;
	}
/**
 * 修改菜单
 */
	@Override
	public int update(Menu t) {
		log.info("修改菜单");
		String sql="update  SHOP_MENU  set menu_name = ? ,menu_describe = ? ,menu_address = ? ,menu_parentid = ? ,menu_type = ?   where menu_id = ? ";
		int count=0;
		try {
			count=qr.execute(sql, t.getMenu_name(),
					 t.getMenu_describe(),	t.getMenu_address(),
					 t.getMenu_parentid(),t.getMenu_type(),t.getMenu_id());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("传入参数["+t.getMenu_name()+","+
				 t.getMenu_describe()+","+	t.getMenu_address()+","+
				 t.getMenu_parentid()+","+t.getMenu_type()+","+t.getMenu_id()+"]");	
		return count;
	}
/**
 * 返回全部菜单
 */
	@Override
	public List<Menu> findAll() {
		log.info("返回全部菜单");
		String sql="select * from SHOP_MENU";
		// TODO Auto-generated method stub
		List<Menu> list=null;
		try {
			list=qr.query(sql, new BeanListHandler<>(Menu.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("没有传入参数");
		return list;
	}
/**
 * 根据 id返回菜单对象
 */
	@Override
	public Menu findById(int id) {
		log.info(" 根据 id返回菜单对象");
		String sql="select * from SHOP_MENU  where menu_id = ?";
		Menu menu=null;
		try {
			menu=qr.query(sql, new BeanHandler<>(Menu.class),id);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("传入参数["+id+"[");
		return menu;
	}
/**
 *   根据 传入的字段返回菜单对象集合
 */
	@Override
	public List<Menu> LikeColumnByValue(String columName, Object value) {
		log.info(" 根据 传入的字段返回菜单对象集合");
		String  sql="select * from SHOP_MENU where" +columName+"="+value;
		List<Menu> list=null;
		try {
			list=qr.query(sql, new BeanListHandler<>(Menu.class));
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("传入参数["+columName+","+value+"[");
		return list;
	}
/**
 * 返回总行数
 */
	@Override
	public int getCount() {
		log.info("返回总行数");
		String sql="select count(1) from SHOP_MENU ";int count=0;
		try {
			count=qr.query(sql, new ScalarHandler<BigDecimal>()).intValue();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("没有传入参数");
		return count;
	}
/**
 * 分页
 */
	@Override
	public List<Menu> findPage(int pageIndex, int pageSize) {
		log.info("分页操作");
		String  sql="\"select *from \"\r\n" + 
				"				+ \"(select rownum r,d.* from \"\r\n" + 
				"				+ \"(select * from SHOP_MENU order by menu_id) d \"\r\n" + 
				"				+ \"where rownum<=?)\"\r\n" + 
				"				+ \" where r>?";
		int max=pageIndex*pageSize;
		int min=(pageIndex-1)*pageSize;
		List<Menu> list=null;
		try {
			list=qr.query(sql, new BeanListHandler<>(Menu.class),max,min);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			log.error(e.toString());
		}
		log.debug("传入参数["+pageIndex+","+pageSize+"[");
		return list;
	}

}
